﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using FileSystem.Models;
using FileSystem.Services.IServices;
using FileSystem.Views;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Xamarin.Forms;

namespace FileSystem.ViewModels {
    /// <summary>
    /// 纸张页ViewModel
    /// </summary>
    public class PaperPageViewModel : ViewModelBase {
        // ******** 构造函数

        /// <summary>
        /// 系统接口
        /// </summary>
        private ISystem _system;

        public PaperPageViewModel(ISystem system) {
            _system = system;
        }

        // ******** 绑定属性
        /// <summary>
        /// 纸张目录项
        /// </summary>
        public DirItem Paper {
            get => _paper;
            set => Set(nameof(Paper), ref _paper, value);
        }

        /// <summary>
        /// 纸张目录项
        /// </summary>
        private DirItem _paper;

        /// <summary>
        /// 纸张内容
        /// </summary>
        public string Content {
            get => _content;
            set => Set(nameof(Content), ref _content, value);
        }

        /// <summary>
        /// 纸张内容
        /// </summary>
        private string _content;

        // ******** 绑定命令
        /// <summary>
        /// 页面显示命令。
        /// </summary>
        private RelayCommand _pageAppearingCommand;

        /// <summary>
        /// 页面显示命令。
        /// </summary>
        public RelayCommand PageAppearingCommand =>
            _pageAppearingCommand ?? (_pageAppearingCommand =
                new RelayCommand(async () =>
                    await PageAppearingCommandFunction()));

        public async Task PageAppearingCommandFunction() {
            Content = await _system.Read(Paper.DirItemName, Paper.Location,
                2048);
        }


        /// <summary>
        /// 文本变化命令
        /// </summary>
        private RelayCommand _textChangeCommand;

        /// <summary>
        /// 文本变化命令
        /// </summary>
        public RelayCommand TextChangeCommand =>
            _textChangeCommand ?? (_textChangeCommand =
                new RelayCommand(async () =>
                    await TextChangeCommandFunction()));

        public async Task TextChangeCommandFunction() {
            var byteLength = Encoding.Default.GetByteCount(Content);

            await _system.Write(Paper.DirItemName,
                FileSystemPageViewModel.CurrentAddress, Content, byteLength);

            await MainPage.DisplayAlert("提示", "保存成功!", "确定");
        }

        // ******** 公开变量

        // ******** 私有变量
        /// <summary>
        /// NavigationPage
        /// </summary>
        public NavigationPage NavigationPage =>
            _navigationPage ?? (_navigationPage =
                Application.Current.MainPage as NavigationPage);

        /// <summary>
        /// NavigationPage
        /// </summary>
        private NavigationPage _navigationPage;

        /// <summary>
        /// MainPage
        /// </summary>
        public MainPage MainPage =>
            _mainPage ?? (_mainPage = NavigationPage.RootPage as MainPage);

        /// <summary>
        /// MainPage
        /// </summary>
        private MainPage _mainPage;

    }
}